home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QRZ! Ham Radio 8
/
QRZ Ham Radio Callsign Database - Volume 8.iso
/
mac
/
files
/
infodata
/
callbook.tar
/
callbook_1.3
/
canadian.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-04-03
|
8KB
|
475 lines
/*
* The Canadian database from the DOC isn't in a very helpful format. They
* don't use standard abbreviations or anything like that and they bunch
* together different fields of information so it's virtually unparsable by
* machine. Anyway, this code tries to take this DOC file and put it in the
* FCC format. A heavy amount of AI (ie guessing) is done to try to get
* things into shape so I can't guarentee it will handle all situations or
* even do the right thing in all cases but it should be close. If it gets
* totally lost, it writes the bad line out so that it may be handled by
* hand. *PLEASE* SEND ME ANY IMPROVEMENTS TO THIS CODE.
*/
#include <ctype.h>
#include <stdio.h>
#define MAXLINE 1024
#define ORG_START 36
#define ADDR_START 71
#define CITY_START 106
#define ZIP_START 141
#define CALL_START 147
char buf[MAXLINE], first[MAXLINE], last[MAXLINE], addr[MAXLINE];
char city[MAXLINE], prov[MAXLINE], zip[MAXLINE], call[MAXLINE];
char *backup(), *rindex();
main(argc, argv)
int argc;
char **argv;
{
FILE *fd;
fd = fopen(argv[1], "r");
while (fgets(buf, MAXLINE, fd) != NULL) {
/* pull the names out of the first field */
if (!extract_names()) {
fprintf(stderr, "%s", buf);
continue;
}
/* address is easy - just copy it over */
strcpyblnks(addr, buf+ADDR_START, CITY_START-ADDR_START);
/* separate the city and province as best you can */
if (!extract_city()) {
fprintf(stderr, "%s", buf);
continue;
}
/* just copy over the zip and the call */
strcpyblnks(zip, buf+ZIP_START, CALL_START-ZIP_START);
strcpyblnks(call, buf+CALL_START, MAXLINE);
printf("%s|%s||%s|||||%s|%s|%s|%s|||||||\n",
call,last,first,addr,city,prov,zip);
}
close(fd);
exit(0);
}
/*
* Figure out the first and last name and load them into the buffers.
*/
extract_names()
{
int count, start, mark1=0, mark2=0, bound;
bound = ORG_START;
count = 0;
while (isspace(buf[count]))
count++;
start = count;
if (count >= ORG_START)
bound = ADDR_START;
while (count < bound) {
while (!isspace(buf[count]))
count++;
mark2 = mark1;
mark1 = count;
while (isspace(buf[count]))
count++;
}
if (!mark1)
return 0;
if (!mark2) {
*first = '\0';
strcpyblnks(last,buf+start,bound-start);
} else {
strcpyblnks(first,buf+start,mark2-start);
strcpyblnks(last,buf+mark2,bound-mark2);
}
return 1;
}
/*
* Figure out the city and province name and load them into the buffers.
*/
extract_city()
{
int is_city = 0;
char *mark, *pnt, *oldpnt;
pnt = backup(buf+ZIP_START);
if (pnt == 0)
pnt = backup(rindex(buf+CITY_START, ','));
if (pnt == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
switch (*pnt) {
case 'a': /* Alberta */
case 'A':
strcpy(prov, "AL");
break;
case 'b': /* New Brunswick or British Columbia */
case 'B':
if (*(pnt+1) == 'c' || *(pnt+1) == 'C') {
strcpy(prov, "BC");
break;
}
if ((pnt = backup(oldpnt = pnt)) == 0) {
strcpy(prov, "BC");
pnt = oldpnt;
}
if (*pnt == 'n' || *pnt == 'N') {
strcpy(prov, "NB");
break;
}
strcpy(prov, "BC");
pnt = oldpnt;
break;
case 'c': /* maybe British Columbia */
case 'C':
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt == 'b' || *pnt == 'B') {
strcpy(prov, "BC");
break;
}
pnt = buf+CITY_START;
goto copy_city;
case 'e': /* maybe Prince Edward Island */
case 'E':
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt == 'p' || *pnt == 'P') {
strcpy(prov, "PE");
break;
}
pnt = buf+CITY_START;
goto copy_city;
case 'f': /* maybe Newfoundland */
case 'F':
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt == 'n' || *pnt == 'N') {
strcpy(prov, "NF");
break;
}
pnt = buf+CITY_START;
goto copy_city;
case 'i': /* maybe Prince Edward Island */
case 'I':
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt != 'e' && *pnt != 'E') {
pnt = buf+CITY_START;
goto copy_city;
}
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt != 'p' && *pnt != 'P') {
pnt = buf+CITY_START;
goto copy_city;
}
strcpy(prov, "PE");
break;
case 'm': /* Manitoba */
case 'M':
strcpy(prov, "MB");
break;
case 'n': /* New Bunswick, Nova Scotia, Newfoundland */
case 'N': /* or the NorthWest Territory! */
if (*(pnt+1) == 'b' || *(pnt+1) == 'B') {
strcpy(prov, "NB");
break;
}
if (*(pnt+1) == 's' || *(pnt+1) == 'S') {
strcpy(prov, "NS");
break;
}
if (*(pnt+1) == 'f' || *(pnt+1) == 'F') {
strcpy(prov, "NF");
break;
}
if (*(pnt+1) == 'w' || *(pnt+1) == 'W') {
strcpy(prov, "NW");
break;
}
if (*(pnt+1) == 'o' || *(pnt+1) == 'O') {
if (*(pnt+2) == 'v' || *(pnt+2) == 'V') {
strcpy(prov, "NS");
break;
}
if (*(pnt+2) == 'r' || *(pnt+2) == 'R') {
strcpy(prov, "NW");
break;
}
pnt = buf+CITY_START;
goto copy_city;
}
if (*(pnt+1) != 'e' && *(pnt+1) != 'E') {
pnt = buf+CITY_START;
goto copy_city;
}
if (*(pnt+2) != 'w' && *(pnt+2) != 'W') {
pnt = buf+CITY_START;
goto copy_city;
}
if (*(pnt+3) == 'b' || *(pnt+3) == 'B') {
strcpy(prov, "NB");
break;
}
if (*(pnt+3) == 'f' || *(pnt+3) == 'F') {
strcpy(prov, "NF");
break;
}
pnt = buf+CITY_START;
goto copy_city;
case 'o': /* Ontario */
case 'O':
strcpy(prov, "ON");
break;
case 'p': /* Prince Edward Island */
case 'P':
strcpy(prov, "PE");
break;
case 'q': /* Quebec */
case 'Q':
strcpy(prov, "PQ");
break;
case 's': /* Nova Scotia or Sasketchewan */
case 'S':
if ((pnt = backup(oldpnt = pnt)) == 0) {
strcpy(prov, "SK");
pnt = oldpnt;
}
if (*pnt == 'n' || *pnt == 'N') {
strcpy(prov, "NS");
break;
}
strcpy(prov, "SK");
pnt = oldpnt;
break;
case 't': /* Yukon or Northwest Territories */
case 'T':
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt == 'y' || *pnt == 'Y') {
strcpy(prov, "YT");
break;
}
if (*pnt == 'n' || *pnt == 'N') {
strcpy(prov, "NW");
break;
}
if (*pnt != 'w' && *pnt != 'W') {
pnt = buf+CITY_START;
goto copy_city;
}
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt == 'n' || *pnt == 'N') {
strcpy(prov, "NW");
break;
}
pnt = buf+CITY_START;
goto copy_city;
case 'w': /* Northwest Territory */
case 'W':
if ((pnt = backup(oldpnt = pnt)) == 0) {
pnt = buf+CITY_START;
goto copy_city;
}
if (*pnt != 'n' && *pnt != 'N') {
pnt = buf+CITY_START;
goto copy_city;
}
strcpy(prov, "NW");
break;
case 'y': /* Yukon */
case 'Y':
strcpy(prov, "YT");
break;
default:
pnt = buf+CITY_START;
goto copy_city;
}
for (oldpnt = buf+CITY_START; oldpnt < pnt; oldpnt++)
is_city |= isalnum(*oldpnt);
copy_city:
if (is_city)
strcpyblnks(city, buf+CITY_START, pnt-buf-CITY_START);
else {
*prov = '\0';
strcpyblnks(city, buf+CITY_START, ZIP_START-CITY_START);
}
for (pnt=city+strlen(city)-1; *pnt==','; pnt--)
*pnt = '\0';
if (strlen(city))
return 1;
return 0;
}
/*
* Back up to the begining of the previous word. Return zero if you hit comma.
*/
char *
backup(str)
char *str;
{
str--;
while (*str != ',' && !isalnum(*str))
str--;
if (*str == ',')
return 0;
while (isalnum(*str))
str--;
return str+1;
}
/*
* copy num bytes from src to dest. While you're at it strip any blanks
* from the beginning and end of the string.
*/
strcpyblnks(dest,src,num)
int num;
char *dest, *src;
{
int num_blanks = 0;
while (*src == ' ') {
src++;
num--;
}
for (; num>0; num--, src++) {
if (*src == '\r' || *src == '\n') {
*dest++ = '\0';
return;
}
if (*src == ' ')
num_blanks++;
else {
for (; num_blanks; num_blanks--)
*dest++ = ' ';
*dest++ = *src;
}
}
*dest = '\0';
}